﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPA_IN201305UV02;
using YLW.Plat.Jhfw.Model.JhfwHzgl;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPA_IN201305UV02
    {
        public static string PatientInfoQuery(GetJhfwHzglListIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //性别
            string sexCode = "";
            string sexName = "";
            switch (jsonData.psex)
            {
                case "B":
                    sexCode = "9";
                    sexName = "未说明的性别";
                    break;
                case "F":
                    sexCode = "2";
                    sexName = "女性";
                    break;
                case "M":
                    sexCode = "1";
                    sexName = "男性";
                    break;
                case "U":
                    sexCode = "0";
                    sexName = "未知的性别";
                    break;
            }

            //创建根对象
            var xmlRoot = new PRPA_IN201305UV02
            {
                ITSVersion = "XML_1.0",
                id1 = new id1 { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPA_IN201305UV02" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = receiver_name
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = sender_name
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",
                    queryByParameter = new queryByParameter
                    {
                        queryId = new queryId { extension = "" },
                        statusCode = new statusCode { code = "new" },
                        initialQuantity = new initialQuantity { value = "2" },
                        matchCriterionList = new matchCriterionList
                        {
                            minimumDegreeMatch = new minimumDegreeMatch
                            {
                                value1 = new value1 { value = "100" },
                                semanticsText = new semanticsText { value = "匹配程度" }
                            }
                        },
                        parameterList = new parameterList
                        {
                            id1 = new id1 { root = "", extension = "" },
                            livingSubjectAdministrativeGender = new value
                            {
                                value1 = new value1
                                {
                                    code = sexCode,
                                    codeSystem = "2.16.156.10011.2.3.3.4",
                                    codeSystemName = "生理性别代码表(GB/T 2261.1)",
                                    Items = new object[]
                                    {
                                        new displayName{value=sexName}
                                    }
                                }
                            },
                            livingSubjectId = new value
                            {
                                value1 = new value1
                                {
                                    Items = new object[]
                                    {
                                        new item{root="2.16.156.10011.1.3",extension=jsonData.pcid}
                                    }
                                }
                            },
                            livingSubjectName = new value
                            {
                                value1 = new value1
                                {
                                    Items = new object[]
                                    {
                                        new item{root="",extension=jsonData.pname}
                                    }
                                }
                            },
                            extension = new extension
                            {
                                page = new value { value1 = new value1 { value = jsonData.page.ToString() } },
                                percount = new value { value1 = new value1 { value = jsonData.percount.ToString() } },
                                hoscode = new value { value1 = new value1 { value = jsonData.hoscode } },
                                mrn = new value { value1 = new value1 { value = jsonData.mrn } },
                                pname = new value { value1 = new value1 { value = jsonData.pname } },
                                psex = new value { value1 = new value1 { value = jsonData.psex } },
                                pcid = new value { value1 = new value1 { value = jsonData.pcid } },
                                jzkh = new value { value1 = new value1 { value = jsonData.jzkh } },
                            }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
